<script setup lang='ts'>
import { ref, inject, onMounted, onBeforeUnmount, watch } from "vue";
import { ESClippingPlane, ESPolygonFlattenedPlane, ESViewShed, ESExcavate, ESBoxClipping, ESVisibilityAnalysis, ESHeightLimitAnalysis, ESClassification } from "esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main";
import { createSceneObjTreeItemFromJson } from "../plotting/esObj/fun";
import RightList from "../../components/RightList.vue";
import Button from "../../components/Button.vue";
import { XbsjEarthUi } from "../../scripts/xbsjEarthUi";
import { getsceneObjNumfromSceneTree } from "../../scripts/general";
const props = withDefaults(defineProps<{ closeSceneobject: number }>(), {})
const xbsjEarthUi = inject('xbsjEarthUi') as XbsjEarthUi
const objType = ref<string>('')
const createlList: { zh: string, type: string, icon: string, leftButton: boolean }[] = [
    {
        zh: '视阈',
        type: 'ESViewShed',
        icon: 'keshiyu',
        leftButton: true
    },
    {
        zh: '挖坑',
        type: 'ESExcavate',
        icon: 'wakeng',
        leftButton: false

    },
    {
        zh: '压平',
        type: 'ESPolygonFlattenedPlane',
        icon: 'yaping',
        leftButton: true

    },
    {
        zh: '裁剪',
        type: 'ESClippingPlane',
        icon: 'jiancai',
        leftButton: false
    },
    {
        zh: '通视分析',
        type: 'ESVisibilityAnalysis',
        icon: 'tongshifenxi',
        leftButton: true
    },
    {
        zh: '体剖切',
        type: 'ESBoxClipping',
        icon: 'tipaoqie',
        leftButton: false
    },
    {
        zh: '控高分析',
        type: 'ESHeightLimitAnalysis',
        icon: 'konggaofenxi',
        leftButton: true
    },
    {
        zh: '倾斜单体化',
        type: 'ESClassification',
        icon: 'jianzhudantihua',
        leftButton: false
    }

];
let sceneObject: ESViewShed | ESExcavate | ESPolygonFlattenedPlane | ESClippingPlane | ESBoxClipping | ESVisibilityAnalysis | ESHeightLimitAnalysis |ESClassification| undefined = undefined
let editingDispose: any = undefined

const createClippingPlane = (item: { zh: string, type: string, icon: string }) => {
    objType.value = item.type
    destroy()
    sceneObject = xbsjEarthUi.createSceneObject(item.type) as ESViewShed | ESExcavate | ESPolygonFlattenedPlane | ESClippingPlane
    if (sceneObject) {
        const sceneObjectNum = getsceneObjNumfromSceneTree(xbsjEarthUi, item.type)
        sceneObject.name = item.zh + (sceneObjectNum + 1)
        sceneObject.editing = true
        editingDispose = (sceneObject.editingChanged.disposableOn(() => {
            if (sceneObject && sceneObject.editing === false) {
                objType.value = ''
                const json = JSON.parse(sceneObject.jsonStr)
                xbsjEarthUi.destroySceneObject(sceneObject)
                sceneObject = undefined
                setTimeout(() => {
                    createSceneObjTreeItemFromJson(xbsjEarthUi, json)
                }, 300)
            }
        }))
    }
}
const destroy = () => {
    if (sceneObject && sceneObject.editing) {
        if (editingDispose) {
            editingDispose()
            editingDispose = undefined
        }
        sceneObject.editing = false
        xbsjEarthUi.destroySceneObject(sceneObject)
        sceneObject = undefined

    }
}
onMounted(() => {
    onBeforeUnmount(() => {
        destroy()
        objType.value = ''
    })
})
watch(() => props.closeSceneobject, (val) => {
    destroy()
    objType.value = ''
}, { immediate: true })
</script>
<template>
    <RightList :title="'空间分析'">
        <Button v-for="item in createlList" :name="item.icon" :content="item.zh"
            :click="() => { createClippingPlane(item) }" :actived="objType === item.type"
            :left-button="item.leftButton"></Button>
    </RightList>
</template>
